package com.chungechunyemen.post.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.chungechunyemen.post.po.Topic;

/**
 * A data access object (DAO) providing persistence and search support for Topic
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.chungechunyemen.post.po.Topic
 * @author MyEclipse Persistence Tools
 */

public class TopicDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(TopicDAO.class);
	// property constants
	public static final String VIEWS = "views";
	public static final String REPLIES = "replies";
	public static final String SUBJECT = "subject";
	public static final String ISTOP = "istop";
	public static final String HOT = "hot";
	public static final String USER_IPORUN = "userIporun";
	public static final String USER_IP = "userIp";
	public static final String LASTUSER_IPORUN = "lastuserIporun";
	public static final String LASTUSER_IP = "lastuserIp";
	public static final String STATE = "state";

	protected void initDao() {
		// do nothing
	}

	public int save(Topic transientInstance) {
		log.debug("saving Topic instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return transientInstance.getTopicId();
	}

	public void delete(Topic persistentInstance) {
		log.debug("deleting Topic instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Topic findById(java.lang.Integer id) {
		log.debug("getting Topic instance with id: " + id);
		try {
			Topic instance = (Topic) getHibernateTemplate().get(
					"com.chungechunyemen.post.po.Topic", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Topic instance) {
		log.debug("finding Topic instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Topic instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Topic as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByViews(Object views) {
		return findByProperty(VIEWS, views);
	}

	public List findByReplies(Object replies) {
		return findByProperty(REPLIES, replies);
	}

	public List findBySubject(Object subject) {
		return findByProperty(SUBJECT, subject);
	}

	public List findByIstop(Object istop) {
		return findByProperty(ISTOP, istop);
	}

	public List findByHot(Object hot) {
		return findByProperty(HOT, hot);
	}

	public List findByUserIporun(Object userIporun) {
		return findByProperty(USER_IPORUN, userIporun);
	}

	public List findByUserIp(Object userIp) {
		return findByProperty(USER_IP, userIp);
	}

	public List findByLastuserIporun(Object lastuserIporun) {
		return findByProperty(LASTUSER_IPORUN, lastuserIporun);
	}

	public List findByLastuserIp(Object lastuserIp) {
		return findByProperty(LASTUSER_IP, lastuserIp);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findAll() {
		log.debug("finding all Topic instances");
		try {
			String queryString = "from Topic";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Topic merge(Topic detachedInstance) {
		log.debug("merging Topic instance");
		try {
			Topic result = (Topic) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Topic instance) {
		log.debug("attaching dirty Topic instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Topic instance) {
		log.debug("attaching clean Topic instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static TopicDAO getFromApplicationContext(ApplicationContext ctx) {
		return (TopicDAO) ctx.getBean("TopicDAO");
	}

	// 按属性值分页查询
	public List findByPropertyAndPage(String propertyName, final Object value,
			final int offset, final int pageSize) {
		final String hql = "from Topic as model where model." + propertyName
				+ "= ? order by model.istop desc,model.lasttime desc";
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				List result = session.createQuery(hql).setParameter(0, value)
						.setFirstResult(offset).setMaxResults(pageSize).list();
				return result;
			}
		});
		return list;
	}

	// 按属性值查询总数
	public int findCountByProperty(String propertyName, final Object value) {
		final String queryString = "select count(*) from Topic as model where model."
				+ propertyName + "= ?";
		List list = getHibernateTemplate().find(queryString, value);
		if (list == null || list.size() == 0 || list.get(0) == null) {
			return 0;
		}
		return ((Long) list.get(0)).intValue();
	}
}